﻿using LibNiconico.NiconicoApi.Extentions;
using LibNiconico.NiconicoApi.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace LibNiconico.NiconicoApi.Internal
{
    /// <summary>
    /// コメントスコアランク
    /// </summary>
    public enum NiconicoCommentScoreRank
    {
        /// <summary>
        /// NGなし～微小
        /// </summary>
        None,

        /// <summary>
        /// 多NG
        /// </summary>
        ManyNg,

        /// <summary>
        /// 中NG
        /// </summary>
        MediumNg,

        /// <summary>
        /// 少NG
        /// </summary>
        LittleNg
    }
    

    /// <summary>
    /// チャットコメントベースクラス
    /// </summary>
    public abstract class NicoChatCommentBase
    {
        /// <summary>
        /// このコメント送信者が184であるか判定
        /// </summary>
        public bool IsAnonymous { get; private set; }

        /// <summary>
        /// コメント受信日時
        /// </summary>
        public DateTime Date { get; private set; }

        /// <summary>
        /// コメントvpos
        /// </summary>
        public int Vpos { get; private set; }

        /// <summary>
        /// mailコマンドリスト
        /// </summary>
        public IEnumerable<string> Mails { get; private set; }

        /// <summary>
        /// コメントNo
        /// </summary>
        public int No { get; private set; }

        /// <summary>
        /// ユーザ属性判定
        /// </summary>
        public NicoChatCommentUser Premium { get; private set; }

        /// <summary>
        /// コメント送信者のユーザID
        /// </summary>
        public string UserId { get; private set; }

        /// <summary>
        /// コメント本文
        /// </summary>
        public string Body { get; private set; }

        /// <summary>
        /// NGスコア
        /// </summary>
        public int Score { get; private set; }

        /// <summary>
        /// コメント送信者のロケール
        /// </summary>
        public string Locale { get; private set; }


        
        /// <summary>
        /// コメントスコアランク
        /// </summary>
        public NiconicoCommentScoreRank CommentScoreRank
        {

            get
            {
                if (this.Score > -1000)
                {
                    return NiconicoCommentScoreRank.None;
                }
                else if (this.Score > -4800)
                {
                    return NiconicoCommentScoreRank.LittleNg;
                }
                else if (this.Score > -10000)
                {
                    return NiconicoCommentScoreRank.MediumNg;
                }
                else
                {
                    return NiconicoCommentScoreRank.ManyNg;
                }
            }
        }

        /// <summary>
        /// コンストラクタ
        /// </summary>
        protected NicoChatCommentBase()
        {
            IsAnonymous = false;
            Mails = new string[0];
            Premium = new NicoChatCommentUser(NicoChatCommentUserType.None, -1);
            UserId = "";
            Body = "";
            Locale = "";
        }

        /// <summary>
        /// コメント解析
        /// </summary>
        /// <param name="comment"></param>
        /// <param name="message"></param>
        protected static void Parse(NicoChatCommentBase comment, XElement message)
        {

            comment.No = XElementExtention.GetAttributeAsInt(message, "no");

            comment.IsAnonymous = XElementExtention.GetAttributeAsBool(message,"anonymity");

            comment.Date = DateTimeExtention.FromUnixTime(XElementExtention.GetAttributeAsLong(message, "date"));

            comment.Vpos = XElementExtention.GetAttributeAsInt(message,"vpos");

            comment.Mails = XElementExtention.GetAttributeAsString(message,"mail").Split(' ');

            comment.Premium = NicoChatCommentUser.Craete(XElementExtention.GetAttributeAsInt(message, "premium"));

            comment.UserId = XElementExtention.GetAttributeAsString(message,"user_id");

            comment.Locale = XElementExtention.GetAttributeAsString(message, "locale");

            comment.Score = message.GetAttributeAsInt("score");

            comment.Body = XElementExtention.GetValueAsString(message.Value);

            

            
        }
    }
}
